package com.fhao.forum.common;

import com.fasterxml.jackson.annotation.JsonInclude;

/**
 * 定义统一结果返回类
 * @param <T>
 */
public class AppResult<T> {
    @JsonInclude(JsonInclude.Include.ALWAYS) //不论任何情况都要参与JSON序列化
    private int code;//状态码
    @JsonInclude(JsonInclude.Include.ALWAYS) //不论任何情况都要参与JSON序列化
    private String message;
    @JsonInclude(JsonInclude.Include.ALWAYS) //不论任何情况都要参与JSON序列化
    T data;

    /**
     * 成功
     * @return
     */
    public static AppResult success() {
        return new AppResult(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage());
    }

    public static <T> AppResult<T> success(T data) {
        return new AppResult<>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage(),data);
    }

    public static AppResult success (String message) {
        return new AppResult(ResultCode.SUCCESS.getCode(), message);
    }

    public static <T> AppResult<T> success (String message,T data) {
        return new AppResult<>(ResultCode.SUCCESS.getCode(), message, data);
    }

    /**
     * 失败
     * @return
     */

    public static AppResult failed() {
        return new AppResult(ResultCode.FAILED.getCode(), ResultCode.FAILED.getMessage());
    }

    public static AppResult failed(String message) {
        return new AppResult(ResultCode.FAILED.getCode(), message);
    }

    public static AppResult failed(ResultCode resultCode) {
        return new AppResult(resultCode.getCode(), resultCode.getMessage());
    }
    public AppResult(int code, String message) {
        this.code = code;
        this.message = message;
    }

    public AppResult(int code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public int getCode() {
        return code;
    }

    public String getMessage() {
        return message;
    }

    public T getData() {
        return data;
    }
}
